package com.jy.novel.controller.system;

import com.jy.novel.common.CommonResult;
import com.jy.novel.controller.base.BaseController;
import com.jy.novel.pojo.system.RoleInfo;
import com.jy.novel.service.system.RoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by yangqy on 4/29/16.
 */
@Controller
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

//    @Autowired
//    ExtWebManagerFrameworkCfg cfgService;
//    @Value("#{config[\'system.defaultRoleId\']}")
    long defaultRoleId;

    @RequestMapping(
            value = {"/system/role/manager"},
            method = {RequestMethod.GET}
    )
    public String manager() {
        return "system/role_manager";
    }

    @RequestMapping(
            value = {"/system/role/list"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public List list() {
//        List list = this.roleService.findRoleList(this.cfgService.getAdminRoleId(), this.cfgService.getDefaultRoleId());
        return null;
    }

    @RequestMapping(
            value = {"/system/role/details"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public RoleInfo details(@RequestParam(
            value = "id",
            required = true
    ) String id) {
        RoleInfo role = this.roleService.getRoleInfo(Long.valueOf(NumberUtils.toLong(id)));
        return role;
    }

    @RequestMapping(
            value = {"/system/role/add"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult roleAdd(@RequestParam(
            value = "name",
            required = true
    ) String name, @RequestParam(
            value = "des",
            required = true
    ) String des) {
        if(StringUtils.isBlank(name)) {
            return this.returnFailure("请输入角色名", new Object[0]);
        } else if(StringUtils.isBlank(des)) {
            return this.returnFailure("请输入描述", new Object[0]);
        } else if(this.roleService.isExistRole(name, des)) {
            return this.returnFailure("创建角色失败，角色名与描述已经存在", new Object[0]);
        } else {
            RoleInfo role = new RoleInfo();
            role.setName(name);
            role.setRemark(des);
            boolean bool = this.roleService.addRole(role);
            return this.returnBoolWrap(bool, "服务器异常");
        }
    }

    @RequestMapping(
            value = {"/system/role/clone"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult roleClone(@RequestParam(
            value = "sId",
            required = true
    ) String sId, @RequestParam(
            value = "name",
            required = true
    ) String name, @RequestParam(
            value = "des",
            required = true
    ) String des) {
//        if(StringUtils.isBlank(name)) {
//            return this.returnFailsWrapper("请输入角色名", new Object[0]);
//        } else if(StringUtils.isBlank(des)) {
//            return this.returnFailsWrapper("请输入描述", new Object[0]);
//        } else {
//            long roleId = NumberUtils.toLong(sId);
//            if(roleId <= 0L) {
//                return this.returnFailsWrapper("角色Id错误", new Object[0]);
//            } else if((long)this.cfgService.getAdminRoleId() == roleId) {
//                return this.returnFailsWrapper("该角色不允许克隆", new Object[0]);
//            } else if(!this.roleService.isExistRole(Long.valueOf(roleId))) {
//                return this.returnFailsWrapper("克隆角色不存在", new Object[0]);
//            } else if(this.roleService.isExistRole(name, des)) {
//                return this.returnFailsWrapper("克隆角色失败，角色名与描述已经存在", new Object[0]);
//            } else {
//                RoleInfo role = new RoleInfo();
//                role.setName(name);
//                role.setDes(des);
//                role.setCanDelete(Boolean.valueOf(true));
//                boolean bool = this.roleService.cloneRole(roleId, role);
//                return this.returnBoolWrapper(bool, "服务器异常", new Object[0]);
//            }
//        }
        return null;
    }

    @RequestMapping(
            value = {"/system/role/edit"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult roleEdit(@RequestParam(
            value = "name",
            required = true
    ) String name, @RequestParam(
            value = "sId",
            required = true
    ) String sId, @RequestParam(
            value = "des",
            required = false
    ) String des) {
        if(StringUtils.isBlank(name)) {
            return this.returnFailure("请输入角色名", new Object[0]);
        } else if(StringUtils.isBlank(sId)) {
            return this.returnFailure("请传入sId", new Object[0]);
        } else {
            long roleId = NumberUtils.toLong(sId);
            if(roleId <= 0L) {
                return this.returnFailure("角色Id错误", new Object[0]);
            } else {
                RoleInfo sRole = this.roleService.getRoleInfo(Long.valueOf(roleId));
                if(sRole == null) {
                    return this.returnFailure("编辑角色失败，原角色不存在", new Object[0]);
                } else {
                    if(!name.equals(sRole.getName()) || !des.equals(sRole.getRemark())) {
                        des = sRole.getRemark();

                        if(this.roleService.isExistRole(name, des)) {
                            return this.returnFailure("该角色名与描述已经存在", new Object[0]);
                        }
                    }

                    boolean bool = this.roleService.updateRoleInfo(Long.valueOf(roleId), name, des);
                    return this.returnBoolWrap(bool, "系统异常");
                }
            }
        }
    }

    @RequestMapping(
            value = {"/system/role/delete"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult roleDelete(@RequestParam(
            value = "id",
            required = true
    ) String id) {
        if(StringUtils.isBlank(id)) {
            return this.returnFailure("缺少参数", new Object[0]);
        } else {
            RoleInfo role = this.roleService.getRoleInfo(Long.valueOf(NumberUtils.toLong(id, -1L)));
            if(role == null) {
                return this.returnDataWrap(new Object[0]);
            } else {
                boolean bool = this.roleService.deleteRole(Long.valueOf(NumberUtils.toLong(id)), Long.valueOf(this.defaultRoleId));
                return this.returnBoolWrap(bool, "删除失败");
            }
        }
    }
}
